﻿namespace Ex03.GarageLogic
{
    public class ElectricEngine
    {
        private float m_RemainingBatteryTime;
        private float m_MaximumBattreyTime;

        public ElectricEngine(float i_RemainingBatteryTime, float i_MaximumBattreyTime)
        {
            if (i_RemainingBatteryTime > i_MaximumBattreyTime)
            {
                throw new ValueOutOfRangeException("Electric Engine", 0.0f, i_MaximumBattreyTime);
            }

            RemainingBatteryTime = i_RemainingBatteryTime;
            MaximumBattreyTime = i_MaximumBattreyTime;
        }

        public void Recharge(float i_TimeToAdd, ref Vehicle io_Vehicle)
        {
            if (i_TimeToAdd + RemainingBatteryTime > MaximumBattreyTime)
            {
                throw new ValueOutOfRangeException("Electric Engine", 0.0f, MaximumBattreyTime - RemainingBatteryTime);
            }

            RemainingBatteryTime += i_TimeToAdd;
            try
            {
                io_Vehicle.RemainingEnergyPercentage = ((RemainingBatteryTime / MaximumBattreyTime) * 100).ToString();
            }
            catch (ValueOutOfRangeException)
            {
                throw new ValueOutOfRangeException("Electric Engine", 0.0f, MaximumBattreyTime - RemainingBatteryTime);
            }
        }

        public float RemainingBatteryTime
        {
            get { return m_RemainingBatteryTime; }
            private set { m_RemainingBatteryTime = value; }
        }

        public float MaximumBattreyTime
        {
            get { return m_MaximumBattreyTime; }
            set { m_MaximumBattreyTime = value; }
        }

        public override string ToString()
        {
            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

            stringBuilder.Append("Remaining Battery Time: ");
            stringBuilder.AppendLine(RemainingBatteryTime.ToString());
            stringBuilder.Append("Maximum Battery Time: ");
            stringBuilder.AppendLine(MaximumBattreyTime.ToString());

            return stringBuilder.ToString();
        }
    }
}
